/**
 * Created by baixiujiang on 17/4/20.
 */
define(['jquery', 'underscore', 'backbone', 'bootstrap_switch',
// Main template for this view
    'dialog',
    'text!views/$!{moduleName}/result.html'], function ($, _, Backbone,
                                              bootstrap_switch, dialog,
                                              resultTemplate) {

    var ModuleNode = Backbone.Model.extend({
        defaults: {
            idAttribute: "moduleId",
            //id : 0,
            code: '',
            name: '',
            link: '',
            target: '',
            orderIndex: '',
            isOuter: 0,
            parentId: '',
            effective: '',
        },
        urlRoot: "module/tree",
    });

    var ResultCollection = Backbone.Collection.extend({
        models: ModuleNode,
        url: '/$!{moduleName}/page/1/10',
        initialize: function () { // Deferred load
        }
    });
    var ResultView = Backbone.View.extend({
        el: '#main-content .sub-container',
        initialize: function () {
            this.collection = new ResultCollection();
            //this.listenTo(this.model, 'change', this.render);
            //this.listenTo(this.model, 'destroy', this.remove);
        },
        events: {
            'click .btn_add_channel': 'add_module',
            'submit form': 'form',
            'click .btn_query': 'query',
            'click .btn_update': 'btn_update',
            'click .btn_delete': 'btn_delete'
        },
        btn_delete: function () {
            var thiz = this;
            dialog.confirm($.i18n.prop('delete_confirm'), function (yes) {
                if (yes) {
                    var model = new ModuleNode({
                        id: thiz.model.attributes.moduleId
                    });
                    model.destroy({
                        success: function (model, response) {
                            window.mainView.reload();
                        }
                    });
                }
            });
        },
        btn_update: function (event) {
            var _this = this;
            var data = {};
            dialog.dialog({
                template: updateTemplate,
                collection: this.collection,
                url: 'module/' + $(event.target).attr('data-id'),
                afterOpen: function () {
                    $('.make-switch').bootstrapSwitch();
                },
                title: "编辑模块",
                className: "update_module",
                save: function () {
                    var switch1 = $('.update_module input[name=effective]').bootstrapSwitch('state');
                    _this.model.attributes.effective = switch1 ? 1 : 0;
                    _this.model.attributes.name = $('input[name=name]').val();
                    _this.model.attributes.parentId = $('input[name=parentId]').val();
                    _this.model.attributes.link = $('input[name=link]').val();
                    _this.model.attributes.target = $('input[name=target]').val();
                    _this.model.idAttribute = 'moduleId';
                    _this.model.id = $('input[name=id]').val();
                    var a = thiz.model.save(null, {
                        success: function () {
                            window.mainView.reload();
                        }
                    });
                    return a;
                }
            });
        },
        render: function (el) {
            this.el = el;
            var _this = this;

            this.collection.fetch({
                success: function (collection, resp) {
                    var data = {};
                    data.page = collection.models[0];
                    var content = _.template(resultTemplate, data);
                    _this.el.html(content);
                    $('.make-switch').bootstrapSwitch();
                    console.log(_this.el);
                    console.log(_this.el.html());
                }
            });

            return this;
        }
    });
    return new ResultView();
});